/**
* Copyright (c) 2013-2014, qtone Inc. All Rights Reserved.
*
* @author: tanghuaicheng
* Project Name: testuc
* $Id:   2017-01-11 10:54:07 $
*/
package com.bangcommunity.bbframe.common.base;

import java.io.Serializable;

public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = -7684836452637484092L;
    protected boolean success = true;
    protected String resultCode;
    protected String message;
    protected Object attach;
    private T model;
    protected long timestampMs = System.currentTimeMillis();

    public BaseResult() {
    }

    public static <T> BaseResult<T> success() {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setResultCode(ResultCode.SUCCESS.getCode());
        return baseResult;
    }

    public static <T> BaseResult<T> success(T model) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setResultCode(ResultCode.SUCCESS.getCode());
        baseResult.setModel(model);
        return baseResult;
    }
    public static <T> BaseResult<ModelData<T>> successModel(T model) {
        BaseResult<ModelData<T>> baseResult = new BaseResult<>();
        baseResult.setResultCode(ResultCode.SUCCESS.getCode());
        baseResult.setModel(new ModelData(model));
        return baseResult;
    }

    public static <T> BaseResult<T> fail(String message) {
        return fail(ResultCode.OTHER.getCode(), message, null);
    }

    public static <T> BaseResult<T> fail(String resultCode, String message) {
        return fail(resultCode, message, null);
    }

    public static <T> BaseResult<T> fail(String message, Object attach) {
        return fail(ResultCode.OTHER.getCode(), message, attach);
    }

    public static <T> BaseResult<T> fail(String resultCode, String message, Object attach) {
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(false);
        baseResult.setResultCode(resultCode);
        baseResult.setMessage(message);
        baseResult.setAttach(attach);
        return baseResult;
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public long getTimestampMs() {
        return timestampMs;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getAttach() {
        return attach;
    }

    public void setAttach(Object attach) {
        this.attach = attach;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }

    @Override
    public String toString() {
        return "BaseResult{" + "success=" + success + ", resultCode=" + resultCode + ", message='" + message + '\''
                + ", model=" + model + '}';
    }
}